//
//  TableViewNoNetWorkView.m
//  YWBaseFrameWork
//
//  Created by 余伟 on 2017/1/13.
//  Copyright © 2017年 NeiQuan. All rights reserved.
//

#import "TableViewNoNetWorkView.h"
#define NO_WIFI_WORDS                   @"网络不太顺畅喔~"

float const width_displayNoWifiView  = 200.0 ;
float const height_displayNoWifiView = 150.0 ;

float const width_labelshow          = 300.0 ;
float const height_labelshow         = 35.0 ;
float const fontSize_labelshow       = 17.0 ;

float const flexY_lb_bt              = 10.0 ;

float const width_bt                 = 100.0 ;
float const height_bt                = 30.0 ;
float const fontSize_bt              = 15.0 ;
@interface TableViewNoNetWorkView ()
@property (nonatomic, strong) UIImageView  *nowifiImgView ;
@property (nonatomic, strong) UILabel      *describeLable;
@property (nonatomic, strong) UIButton     *reloadButton ;
@property (nonatomic, copy) ReloadButtonClickBlock reloadButtonClickBlock ;
@end
@implementation TableViewNoNetWorkView

- (void)showInView:(UIView *)viewWillShow
{
    [viewWillShow addSubview:self];
    
}
- (void)dismiss
{
    [self removeFromSuperview] ;
}
- (instancetype)initWithFrame:(CGRect)frame
                  reloadBlock:(ReloadButtonClickBlock)reloadBlock {
    self = [super initWithFrame:frame];
    if (self) {
        self.reloadButtonClickBlock = reloadBlock ;
        [self setup] ;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews] ;
    CGRect rectWifi = CGRectZero ;
    rectWifi.size = CGSizeMake(width_displayNoWifiView, height_displayNoWifiView) ;
    rectWifi.origin.x = (self.frame.size.width - width_displayNoWifiView) / 2.0 ;
    rectWifi.origin.y = (self.frame.size.height - height_displayNoWifiView - height_labelshow - flexY_lb_bt - height_bt) / 2.0 ;
    self.nowifiImgView.frame = rectWifi ;
    
    CGRect rectLabel = CGRectZero ;
    rectLabel.origin.x = (self.frame.size.width - width_labelshow) / 2.0 ;
    rectLabel.origin.y = rectWifi.origin.y + rectWifi.size.height ;
    rectLabel.size = CGSizeMake(width_labelshow, height_labelshow) ;
    self.describeLable.frame = rectLabel ;
    
    CGRect rectButton = CGRectZero ;
    rectButton.origin.x = (self.frame.size.width - width_bt) / 2.0 ;
    rectButton.origin.y = rectLabel.origin.y + rectLabel.size.height + flexY_lb_bt ;
    rectButton.size = CGSizeMake(width_bt, height_bt) ;
    self.reloadButton.frame = rectButton ;
}

- (void)setup
{
    [self configure] ;
    [self nowifiImgView] ;
    [self describeLable] ;
    [self reloadButton] ;
}

- (void)configure
{
    self.backgroundColor = [UIColor whiteColor] ;
}

- (UIImageView *)nowifiImgView
{
    if (!_nowifiImgView)
    {
        _nowifiImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"network_xinhao_"]] ;
        _nowifiImgView.contentMode = UIViewContentModeScaleAspectFit ;
        if (![_nowifiImgView superview]) {
            [self addSubview:_nowifiImgView] ;
        }
    }
    return _nowifiImgView ;
}
- (UILabel *)describeLable
{
    if (!_describeLable) {
        _describeLable = [[UILabel alloc] init] ;
        _describeLable.text = NO_WIFI_WORDS ;
        _describeLable.font = [UIFont boldSystemFontOfSize:fontSize_labelshow] ;
        _describeLable.textAlignment = NSTextAlignmentCenter ;
        _describeLable.textColor = [UIColor darkGrayColor] ;
        if (![_describeLable superview]) {
            [self addSubview:_describeLable] ;
        }
    }
    return _describeLable ;
}

- (UIButton *)reloadButton
{
    if (!_reloadButton) {
        _reloadButton = [[UIButton alloc] init] ;
        [_reloadButton setTitle:@"重新加载" forState:0] ;
        [_reloadButton setTitleColor:[UIColor darkGrayColor] forState:0] ;
        _reloadButton.titleLabel.font = [UIFont systemFontOfSize:fontSize_bt] ;
        _reloadButton.layer.cornerRadius = 5.0f ;
        _reloadButton.layer.borderWidth = 1.0f ;
        _reloadButton.layer.borderColor = [UIColor darkGrayColor].CGColor ;
        [_reloadButton addTarget:self action:@selector(reloadButtonClicked) forControlEvents:UIControlEventTouchUpInside] ;
        if (![_reloadButton superview]) {
            [self addSubview:_reloadButton] ;
        }
    }
    return _reloadButton ;
}

- (void)reloadButtonClicked {
    self.reloadButtonClickBlock() ;
}
@end
